﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcContrib.Pagination;
using Project.DAL.Repository;

namespace Project.WebUI.Areas.Admin.Controllers
{
    public class WithDrawController : Controller
    {
        #region "private"
        private readonly IUserRepository _userRepository;
        private readonly IDepositRepository _depositRepository;
        private readonly IWithDrawRepository _withDrawRepository;
        private readonly IUserService _userService;
        private readonly ICountryRepository _countryRepository;
        private readonly IContactRepository _contactRepository;        
        private readonly IStoreSecureRepositoty _storeSecureRepositoty;

        public WithDrawController(
            IUserRepository userRepository,
            IDepositRepository depositRepository,
            IWithDrawRepository withDrawRepository,
            IUserService userService,
            ICountryRepository countryRepository,
            IContactRepository contactRepository,            
            IStoreSecureRepositoty storeSecureRepositoty)
        {
            _userRepository = userRepository;
            _depositRepository = depositRepository;
            _withDrawRepository = withDrawRepository;
            _userService = userService;
            _countryRepository = countryRepository;
            _contactRepository = contactRepository;            
            _storeSecureRepositoty = storeSecureRepositoty;
        }

        #endregion   
     

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LoadAjax(int page)
        {
            var listwithdraw = _withDrawRepository.GetMany(null,
                                                           d =>
                                                           d.OrderByDescending(t => t.CreatedTime).ThenBy(t => t.Amount),
                                                           "User");
            ViewBag.page = page;
            ViewBag.result = listwithdraw.Count();
            
            if (page == 1)
            {
                ViewBag.start = 0;
            }
            else
            {
                ViewBag.start = (page - 1) * 10;
            }
            return PartialView("_MainContent", listwithdraw.AsPagination(page, 10));
        }

        #region "UpdateStatus"
        public ActionResult UpdateStatus(int id, int status)
        {
            var wd = _withDrawRepository.GetById(id);
            if (status == 1)
            {
                wd.Approved = false;
                _userRepository.SaveChanges();
                ViewBag.status = false;
                ViewBag.id = id;
                ViewBag.controller = "WithDraw";
                return PartialView("_UpdateStatus");
            }
            wd.Approved = true;
            _withDrawRepository.SaveChanges();
            ViewBag.status = true;
            ViewBag.id = id;
            ViewBag.controller = "WithDraw";
            return PartialView("_UpdateStatus");
        }

        #endregion

    }
}
